/**   
* @Title: PerformanceRecord.java
* @Package me.paxos.util
* @Description: TODO
* @author xaoyaoyao
* @date Dec 25, 2018 11:54:41 AM
* @version V1.0   
*/
package me.paxos.util;

/**
 * @ClassName: PerformanceRecord
 * @Description: 性能记录
 * @author xaoyaoyao
 * @date Dec 25, 2018 11:54:41 AM
 * 
 */
public class PerformanceRecord {

	private static final int MAX_ID_VALUE = 200;
	private static PerformanceRecord instance = null;
	private String[] despArray;
	private long[] startArray;
	private int recordCount;

	public static PerformanceRecord getInstance() {
		if (null == instance) {
			synchronized (PerformanceRecord.class) {
				if (null == instance)
					instance = new PerformanceRecord();
			}
		}
		return instance;
	}

	private PerformanceRecord() {
		despArray = new String[MAX_ID_VALUE];
		startArray = new long[MAX_ID_VALUE];
		recordCount = 0;
	}

	public void start(String description, int ID) {
		despArray[ID] = description;
		startArray[ID] = System.currentTimeMillis();
	}

	public void end(int ID) {
		long endTime = System.currentTimeMillis();
		String performStr = String.format("%s 耗时为%d ms.", despArray[ID], endTime - startArray[ID]);
		printPerform(performStr);
		// help GC
		despArray[ID] = null;
	}

	public void printPerform(String performStr) {
		System.out.println(performStr);
	}

}
